// pages/createaccount/createaccount.js
const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    exchange_rate:"1.00",
    title:"",
    interest:"0.00",
    use_account:{
      miaosu:"选填",
      id:0
    },
    profit_points:"100.00",
    amount:"0.00",
    customer:{},
    invoice:{},
    name:"",
    desc:"",
    showDate:false,
    showDDLDate:false,
    mindate:Math.round((new Date() - 365*86400*1000)),
    color:"#5779E7",
    type_data:['发票','客户','其他'],
    type_index:2,
    type:"其他",
    bookId:0,
    page_type:1,
    icon:"",
    id:0,
    meet_exchange_rate:"1.0",
    account:{
      id:0
    },
    date:"",
    books:[],
    bookIndex:0,
    bookName:"默认账本",
    sub_type_data:['现金','银行卡','信用卡','应收','应付账'],
    sub_type_index:0,
    sub_type:"现金",
    currency:"RMB",
    currencys:[{
      tag:"RMB",
      name:"人民币"
    },

    {
      tag:"EUR",
      name:"欧元"
    }],
    ddl:"",
    key_name:"tag",
    currencyIndex:0
  },
  onSelectDate(){
    this.setData({
      showDate:true
    })
  },
  getBooks(){
    var that = this;
    request.requestAjax('company/getBooks',{},"加载中",function(res){
        if(res.status==200){
            that.setData({
              books:res.data.books
            });

            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  onSelectDDLDate(){
    this.setData({
      showDDLDate:true
    })
  },
  onDateConfirm(e){
    this.setData({
      date:e.detail.toLocaleString('zh', { hour12: false }),
      showDate:false
    })

    
  },
  onDDLDateConfirm(e){
    this.setData({
      ddl:e.detail.toLocaleString('zh', { hour12: false }),
      showDDLDate:false
    })

    
  },
  onDDLDateClose(e){
    this.setData({
      showDDLDate:false
    })
  },
  onDateClose(e){
    this.setData({
      showDate:false
    })
  },
  bindMultiPickerChange(e) {
    var that = this;
    this.setData({
      bookIndex:e.detail.value,
      bookName: that.data.books[e.detail.value].name,
      bookId:that.data.books[e.detail.value].id
    })

    console.log(that.data.bookIndex,that.data.bookName,that.data.bookId)
  },
  onClose(e){

  },
  onDel(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定删除此账户吗？',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后
          that.doRemove();
        } 
      }
    })
  },
  doRemove(){


    var that = this;
    var data = that.data;
    request.requestAjax('account/removeAccount',{id:that.data.id},"正在移除",function(res){
        if(res.status==200){
          wx.showToast({
            title: '移除成功',
            icon:"success"
          })
          setTimeout(function(){
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 3];
        
            if(Reflect.has(prevPage,"getAccounts")){
              prevPage.getAccounts();
            }
              wx.navigateBack({
                delta:2
              });
          },1000);
            return false;
        }else{
          wx.showToast({
            title: res.msg,
            icon:"none"
          })
        }

    },function(res){
      wx.showToast({
        title: '提交失败',
        icon:"none"
      })
    });
  },
  selectSubTypeScope(events){

    this.setData({
      sub_type_index:events.detail.value,
      sub_type:this.data.sub_type_data[events.detail.value]
    })
    this.onClose()
  },
  selectTypeScope(events){

    this.setData({
      type_index:events.detail.value,
      type:this.data.type_data[events.detail.value]
    })
    this.onClose()
  },
  onSubmit(){
      var that = this;
      var data = that.data;
      console.log(data)
      request.requestAjax('meet_receivable/pay_and_collection',{data:data},"加载中",function(res){
        if(res.status==200){
          wx.showToast({
            title: '保存成功',
            icon:"success"
          })
          setTimeout(function(){
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
        
            if(Reflect.has(prevPage,"getInfo")){
              prevPage.getInfo();
            }
            // if(Reflect.has(prevPage,"getMeetAndReceivable")){
            //   prevPage.getMeetAndReceivable();
            // }

            wx.navigateBack();
          },1000);
            return false;
        }else{
          wx.showToast({
            title: res.msg,
            icon:"none"
          })
        }

    },function(res){
      wx.showToast({
        title: '提交失败',
        icon:"none"
      })
    });
  },
  getCurrency(){
    var that = this;
    request.requestAjax('company/getCurrency',{},"加载中",function(res){
        if(res.status==200){
            that.setData({
              currencys:res.data
            })
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  getInfo(){
    var that = this;
    request.requestAjax('meet_and_receivable/logInfo',{id:this.data.id},"加载中",function(res){
        if(res.status==200){
            console.log(res.data)
            that.setData({
              type_index:res.data.sub_type-1,
              page_type:res.data.type,
              id:res.data.id,
              date:res.data.add_time,
              name:res.data.name,
              desc:res.data.desc,
          
    
              ddl:res.data.handle_date,
   
         
            })
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCurrency();
    this.getBooks();
    this.setData({
      wait_id:options.wait_id,
      page_type:options.page_type
    })
    console.log(this.data.wait_id)
    console.log(this.data.page_type)
  },
  goPage(event){
    // console.log(event);
    if(event.currentTarget.dataset.pageurl=="/pages/register/register"){
      Toast('暂未开放注册~');
    }

    wx.navigateTo({
      url: event.currentTarget.dataset.pageurl,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})